package com.v2future.sjt.collegecheckingattendance.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.library.Constant;
import com.v2future.sjt.collegecheckingattendance.LazyFragment;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.message.adapter.SwipeAdapter;
import com.v2future.sjt.collegecheckingattendance.message.bean.MessageBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;
import com.v2future.sjt.collegecheckingattendance.utils.LogUtils;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.SlideView;
import com.v2future.sjt.collegecheckingattendance.widget.SwipeListView;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.io.Serializable;
import java.util.List;

/**
 * Created by minnie on 16/1/21.
 */
public class MessageFragment extends LazyFragment implements AdapterView.OnItemClickListener,
        PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener{

    private ListView messagesLv;

    private PullToRefreshView pullToRefreshView;

    SwipeAdapter swipeAdapter;

    private List<MessageBean> dataList;

    /**
     * 消息页数
     */
    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        messagesLv = (ListView) view.findViewById(R.id.lv_messages);
        pullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_refresh);
        pullToRefreshView.setFooterViewVisibility(View.GONE);
        pullToRefreshView.setOnFooterRefreshListener(this);
        pullToRefreshView.setOnHeaderRefreshListener(this);
    }

    private void refreshView(){
        swipeAdapter = new SwipeAdapter(getBaseActivity(), dataList, DensityUtil.dip2px(getBaseActivity(), 120));
//        swipeAdapter.setOnRightItemClickListener(this);
        swipeAdapter.setStatusClickListener(new SwipeAdapter.onStatusClickListener() {
            @Override
            public void onStatusClick(String pKApproHistoryId) {
                getBaseActivity().HideDialog();
                getBaseActivity().showLoadingDialog();
                doApproveLeave(pKApproHistoryId);
            }
        });
        messagesLv.setAdapter(swipeAdapter);
//        messagesLv.setOnItemClickListener(this);
        if (dataList.size() == 0) {
            getBaseActivity().tip("没有待处理的消息");
        }
    }

    private void doGetMessageList(final String page, final String rowNum){
        BaseRequestParams params = new BaseRequestParams(getBaseActivity(), getBaseActivity().formatUrl(Constants.MESSAGE_LIST));
        params.addQueryStringParameter("page", page);
        params.addQueryStringParameter("rowNum", rowNum);
        params.addQueryStringParameter("userType", getBaseActivity().getUserInfo().getData().getSourceTypeId());
        NetUtils.doGetRequest(getBaseActivity(), params, new NetTaskListener<MessageResponeBean>() {

            @Override
            public void onSuccess(MessageResponeBean result) {
                dataList = result.getData();
                refreshView();
                isPrepared = true;
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(MessageResponeBean result) {
                getBaseActivity().tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                getBaseActivity().tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                getBaseActivity().cancelLoadingDialog();
                getBaseActivity().cancelTitleLoading();
                pullToRefreshView.onHeaderRefreshComplete();
            }

            @Override
            public void onSuccessCache(MessageResponeBean result) {

            }
        });
    }

    public void doApproveLeave(final String pKApproHistoryId){
        BaseRequestParams params = new BaseRequestParams(getBaseActivity(), getBaseActivity().formatUrl(Constants.APPROVE_LEAVE));
        params.addQueryStringParameter("pKApproHistoryId", pKApproHistoryId);
        NetUtils.doGetRequest(getBaseActivity(), params, new NetTaskListener<BaseResponseBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(BaseResponseBean result) {
                getBaseActivity().showOneBtnDialog(getString(R.string.notice), "审批完成", getString(R.string.iknow), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getBaseActivity().HideDialog();
                        getBaseActivity().showLoadingDialog();
                        doGetMessageList(page+"", "10");
                    }
                });
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(BaseResponseBean result) {
                getBaseActivity().tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                getBaseActivity().tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                getBaseActivity().cancelLoadingDialog();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(BaseResponseBean result) {

            }
        });
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {

    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        doGetMessageList(page+"", "10");
    }

    public class MessageResponeBean extends BaseResponseBean implements Serializable {
        private List<MessageBean> data;

        public List<MessageBean> getData() {
            return data;
        }

        public void setData(List<MessageBean> data) {
            this.data = data;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final MessageBean messageBean = dataList.get(position);
        getBaseActivity().showTwoBtnDialog(getString(R.string.notice), "确定审批此请假信息吗", getString(R.string.comfirm), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().HideDialog();
                getBaseActivity().showLoadingDialog();
                doApproveLeave(messageBean.getPkApproHistoryId());
            }
        }, getString(R.string.cancel), null);
    }

    @Override
    protected void lazyLoad() {
        getBaseActivity().showTitleLoading();
        doGetMessageList(page+"", "10");
    }
}
